import 'package:backend.core/core/domain/entities/web/web_purchased_subscription_transaction_offer_type.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_purchased_subscription_transaction.dart';
import 'package:backend.core/core/domain/entities/web/yookassa/yookassa_transaction_revocation_info.dart';
import 'package:backend.core/core/domain/utils/uuid_generator.dart';
import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_succeeded_payment.dart';
import 'package:backend.purchases/domain/helpers/create_web_money_amount_from_yookassa_api_one.dart';
import 'package:backend.purchases/domain/helpers/yookassa/is_yookassa_api_succeeded_payment_revoked.dart';

abstract class CreateYookassaPurchasedSubscriptionTransactionFromApiOne {
  YookassaPurchasedSubscriptionTransaction call({
    required YookassaApiSucceededPayment apiPayment,
    required String paymentMethodId,
    String? originalTransactionId,
  });
}

class CreateYookassaPurchasedSubscriptionTransactionFromApiOneImpl
    implements CreateYookassaPurchasedSubscriptionTransactionFromApiOne {
  const CreateYookassaPurchasedSubscriptionTransactionFromApiOneImpl({
    required CreateWebMoneyAmountFromYookassaApiOne createWebMoneyAmountFromYookassaApiOne,
    required IsYookassaApiSucceededPaymentRevoked isYookassaApiSucceededPaymentRevoked,
  })  : _createWebMoneyAmountFromYookassaApiOne = createWebMoneyAmountFromYookassaApiOne,
        _isYookassaApiSucceededPaymentRevoked = isYookassaApiSucceededPaymentRevoked;

  final CreateWebMoneyAmountFromYookassaApiOne _createWebMoneyAmountFromYookassaApiOne;
  final IsYookassaApiSucceededPaymentRevoked _isYookassaApiSucceededPaymentRevoked;

  @override
  YookassaPurchasedSubscriptionTransaction call({
    required YookassaApiSucceededPayment apiPayment,
    required String paymentMethodId,
    String? originalTransactionId,
  }) {
    final id = UuidGenerator().generateUuid();

    final amount = _createWebMoneyAmountFromYookassaApiOne(
      yookassaApiMoneyAmount: apiPayment.amount,
    );

    final incomeAmount = _createWebMoneyAmountFromYookassaApiOne(
      yookassaApiMoneyAmount: apiPayment.incomeAmount,
    );

    YookassaTransactionRevocationInfo? revocationInfo;

    final apiPaymentRevoked = _isYookassaApiSucceededPaymentRevoked(
      apiPayment: apiPayment,
    );

    if (apiPaymentRevoked) {
      final currentTime = DateTime.now().toUtc();

      final refundedAmount = _createWebMoneyAmountFromYookassaApiOne(
        yookassaApiMoneyAmount: apiPayment.refundedAmount,
      );

      revocationInfo = YookassaTransactionRevocationInfo(
        time: currentTime,
        refundedAmount: refundedAmount,
      );
    }

    return YookassaPurchasedSubscriptionTransaction(
      id: id,
      originalTransactionId: originalTransactionId ?? id,
      userId: apiPayment.userId,
      purchaseItemSku: apiPayment.purchaseItemSku,
      paymentMethodId: paymentMethodId,
      amount: amount,
      offerType: WebPurchasedSubscriptionTransactionOfferType.regular,
      time: apiPayment.paymentTime,
      yookassaPaymentId: apiPayment.id,
      revocationInfo: revocationInfo,
      incomeAmount: incomeAmount,
      test: apiPayment.test,
    );
  }
}
